<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="catecard">

      <el-row>
        <el-col>
          <el-button type="primary" @click="addcate">添加分类</el-button>
        </el-col>
      </el-row>
      
      <tree-table class="treetable"
      :data="catelist"
      :columns="columns"
      show-index
      index-text="#"
      border
      :show-row-hover="false"
      :expand-type="false"
      :selection-type="false">
        <template slot="isok" slot-scope="scope">          
          <el-icon class="el-icon-success" v-if="scope.row.cat_deleted===false" style="color:lightgreen;"></el-icon>
          <el-icon class="el-icon-error" v-else style="color: red;"></el-icon>
        </template>
        <template slot="degrade" slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.cat_level===0" size="mini">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level===1" size="mini">二级</el-tag>
          <el-tag type="warning" v-else size="mini">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" size="mini" @click="cateedit(scope.row.cat_id)">编辑</el-button>
          <el-button type="danger" size="mini" @click="catedelete(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>

      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3, 5, 8, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
      </el-pagination>
    </el-card>

    <el-dialog
      title="添加分类"
      :visible.sync="addcatedialogVisible"
      width="50%"
      @close="addcateDialogclosed">
      <el-form ref="addcateformref" :model="addcateform" label-width="80px" :rules="addcaterules">
        <el-form-item label="分类名称">
          <el-input v-model="addcateform.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="selectedKeys"
            :options="parantcatelist"
            :props="cascaderProps"
            @change="cascaderChange"
            clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addcatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcateconfirm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="编辑分类"
      :visible.sync="editcatedialogVisible"
      width="50%">
      <el-form ref="editcateformref" :model="editcateform" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="editcateform.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editcatedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editcateconfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Goodssort",
  data(){
    return {
      catelist: [],
      total: 0,
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: '商品分类',
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "等级",
          type: "template",
          template: "degrade"
        },
        {
          label: "操作",
          type: "template",
          template: "opt"
        }
      ],

      // 添加分类对话框数据
      addcatedialogVisible: false,
      addcateform: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0,
      },
      addcaterules:{
        cat_name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' }
        ],
      },
      parantcatelist: [],
      selectedKeys: [],
      cascaderProps: {
        value: 'cat_id',
        label: "cat_name",
        children: "children",
        expandTrigger:"hover",
        checkStrictly: true,
      },

      //编辑分类数据
      editcatedialogVisible: false,
      editcateform: {
        cat_name: ''
      }
      
    }
  },
  created() {
    this.getcatelist();
  },
  methods: {
    async getcatelist(){
      const {data : res} = await this.$http.get("/categories",{
        params: this.queryInfo
      })
      if(res.meta.status !== 200) return this.$message.error("获取商品分类失败")
      this.catelist = res.data.result;
      this.$message.success("获取商品分类成功");
      // console.log(res);
      this.total = res.data.total;
    },

    // 分页相关方法
    handleSizeChange(newsize){
      this.queryInfo.pagesize = newsize;
      this.getcatelist();
    },
    handleCurrentChange(newpage){
      this.queryInfo.pagenum = newpage;
      this.getcatelist();
    },

    // 添加分类相关方法
    addcate(){
      this.getparentcatelist();
      this.addcatedialogVisible = true;
    },

    async getparentcatelist(){
      const{ data: res} = await this.$http.get("/categories?type=2");
      this.parantcatelist = res.data;
      console.log(res);
    },

    cascaderChange(){
      if(this.selectedKeys.length > 0){
        // console.log(this.selectedKeys,"test");
        this.addcateform.cat_pid = this.selectedKeys[this.selectedKeys.length-1];
        this.addcateform.cat_level = this.selectedKeys.length;
        return
      }else{
        // console.log(this.selectedKeys,"test2");
        this.addcateform.cat_pid=0;
        this.addcateform.cat_level=0;
      }
      
    },

    async addcateconfirm(){
      const {data: res} = await this.$http.post("/categories",this.addcateform);
      if(res.meta.status !== 201) return this.$message.error("添加分类失败");
      this.$message.success("添加分类成功");
      this.getcatelist();
      this.addcatedialogVisible = false;
    },
    addcateDialogclosed(){
      this.$refs.addcateformref.resetFields();
      // console.log(this.$refs.addcateformref.resetFields);
      this.selectedKeys = [];
      this.addcateform.cat_pid = 0;
      this.addcateform.cat_level = 0;
    },
    
    //编辑和删除
    async cateedit(id){
      const {data: res} = await this.$http.get("/categories/"+id);
      // console.log(id);
      this.editcateform = res.data;
      this.editcatedialogVisible = true;
    },

    async editcateconfirm(){
      const {data: res} = await this.$http.put("/categories/"+this.editcateform.cat_id,{
        cat_name: this.editcateform.cat_name
      })
      if(res.meta.status !== 200) return this.$message.error("更改商品分类名称失败")
      this.$message.success("更改商品分类名称成功")
      this.getcatelist();
      this.editcatedialogVisible = false;
      // console.log(res);
    },

    async catedelete(id){
      const {data: res} = await this.$http.delete("/categories/"+id);
      this.getcatelist();
      if(res.meta.status !== 200) return this.$message.error("删除失败");
      this.$message.success("删除成功");
    }

  },
}
</script>

<style scpoed>
  .catecard{
    margin-top: 30px;
  }

  .treetable{
    margin-top: 20px;
  }
  
  .el-cascader{
    width: 100%;
  }
</style>